
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from">

                    <div class="layui-form-item">
                        <label class="layui-form-label">用户ID</label>
                        <div class="layui-input-block">
                            <input type="text" name="user_id" value="" id="user_id" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">订单号</label>
                        <div class="layui-input-block">
                            <input type="text" name="order_no" value="" id="order_no" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">下单时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="created_time[]" value="" id="start_time" class="layui-input">
                            <input type="text" name="created_time[]" value="" id="end_time" class="layui-input">
                        </div>
                    </div>

<!--                    <div class="layui-form-item">-->
<!--                        <label class="layui-form-label">订单类型</label>-->
<!--                        <div class="layui-input-block">-->
<!--                            <select name="type" id="type" class="layui-input">-->
<!--                                <option value="">请选择</option>-->
<!--                                <option value="1">普通订单</option>-->
<!--                                <option value="2">报单订单</option>-->
<!--                            </sele`ct>-->
<!--                        </div>-->
<!--                    </div>-->

                    <div class="layui-form-item">
                        <label class="layui-form-label">支付状态</label>
                        <div class="layui-input-block">
                            <select name="pay_time[]" id="is_pay" class="layui-input">
                                <option value="">请选择</option>
                                <option value="null">未付款</option>
                                <option value="not null">已付款</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">支付方式</label>
                        <div class="layui-input-block">
                            <select name="pay_way" id="pay_way" class="layui-input">
                                <option value="">请选择</option>
                                <option value="wechat">微信</option>
                                <option value="balance">余额</option>
                                <option value="paypal">paypal</option>
                            </select>
                        </div>
                    </div>



                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label"></label>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="getData">导出全部数据</button>
            </div>
        </script>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            {{# if(d.pay_time && !d.shipping_time && !d.cancel_time) {}}
<!--            <button class="pear-btn pear-btn-xs tool-btn" lay-event="shipping" permission="app.admin.order.update">配货单</button>-->
            {{# } else {}}
<!--            <button class="pear-btn pear-btn-xs tool-btn" lay-event="shipping" permission="app.admin.order.update">详情</button>-->
            {{# } }}

            {{# if(d.refund_status == 1) {}}
<!--            <button class="pear-btn pear-btn-xs tool-btn" lay-event="refund" permission="app.admin.order.update">去审核退款</button>-->
            {{# } }}
        </script>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        
        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/order/select?type[]=5";
            const EXPORT_API = "/app/admin/order/export?type[]=5";
            const UPDATE_API = "/app/admin/order/update";
            const DELETE_API = "/app/admin/order/delete";
            const SHOW_API = "/app/admin/order/show";
            const SHOW_EXPRESS_API = "/app/admin/order/express";
            const INSERT_URL = "/app/admin/order/insert";
            const UPDATE_URL = "/app/admin/order/update";

            layui.use('laydate', function() {
                var laydate = layui.laydate;

                laydate.render({
                    elem: "#start_time",
                    type: "datetime"
                })

                laydate.render({
                    elem: "#end_time",
                    type: "datetime"
                })
            })

            function showExpress(orderNo) {
                layui.use(["table", "form", "common", "popup", "util"], function() {
                    let common = layui.common;
                    layer.open({
                        type: 2,
                        title: "新增",
                        shade: 0.1,
                        shadeClose: true,
                        area: [common.isModile() ? "100%" : "500px", common.isModile() ? "100%" : "450px"],
                        content: SHOW_EXPRESS_API + "?order_no=" + orderNo
                    });
                });
            }

            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util", "element"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                let element = layui.element;

                console.log(element);
                
				// 表头参数
				let cols = [
					{
						type: "checkbox",
						align: "center"
					},{
						title: "id",align: "center",
						field: "id",
                        width: 80,
					},{
                        title: "用户ID",align: "center",
                        field: "user_id",
                        width: 80,
                    },{
                        title: "模板名称",align: "center",
                        field: "work_info.name",
                        width: 200,
                        templet: function(row) {
                            return row.work_info.name;
                        }
                    },{
						title: "订单号",align: "center",
						field: "order_no",
                        width: 200,
					},{
						title: "订单金额",align: "center",
						field: "price",
                        width: 120,
					},{
						title: "支付方式",align: "center",
						field: "pay_way_text",
                        width: 150,
					},{
						title: "支付时间",align: "center",
						field: "pay_time",
                        width: 180,
					},{
                        title: "订单类型",align: "center",
                        field: "type_text",
                        width: 100,
                    },{
						title: "创建时间",align: "center",
						field: "created_time",
                        width: 180,
					},{
						title: "操作",
						toolbar: "#table-bar",
						align: "center",
						fixed: "right",
						width: 250,
					}
				];
				
				// 渲染表格
				table.render({
				    elem: "#data-table",
				    url: SELECT_API,
				    page: true,
				    cols: [cols],
				    skin: "line",
				    size: "lg",
				    toolbar: "#table-toolbar",
				    autoSort: false,
				    defaultToolbar: [{
				        title: "刷新",
				        layEvent: "refresh",
				        icon: "layui-icon-refresh",
				    }, "filter", "print", "exports"],
				    done: function () {
				        layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
				    }
				});


				
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "edit") {
                        edit(obj);
                    } else if (obj.event == 'shipping') {
                        shipping(obj)
                    } else if (obj.event == 'refund') {
                        let url = '/app/admin/order-refund/index?order_no=' + obj.data.order_no;
                        let title = '订单退款';

                        parent.layui.tab.addTabOnlyByElem("content", {
                            id: 227,
                            title: title,
                            url: url,
                            close: true,
                        },0)


                        // top.layui.tab.tabAdd("content",{id:Math.floor(Math.random()*10000),title:title,url:url,close:"允许关闭"});

                    } else if (obj.event == 'setShippingFee') {
                        setShippingFee(obj)
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        add();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        batchRemove(obj);
                    } else if (obj.event === 'getData') {
                        let data = $('#search-form').serialize();
                        $.ajax({
                            type: 'GET',
                            url: EXPORT_API + '?' + data, //因为后端接口分页了，在不添加接口的情况下使用
                            contentType: "application/json",
                            success: function (res) {
                                let title = []
                                for (let i in cols) {
                                    if (i > 0 && i < cols.length-1) {
                                        title.push(cols[i].title)
                                    }
                                }

                                console.log(title)

                                let data = []

                                for (let i in res.data) {
                                    let itemData = []
                                    for (let j in cols) {
                                        if (j > 0 && j < cols.length-1) {

                                            let value = 0;
                                            if (cols[j].field.includes('.')) {
                                                let arr = cols[j].field.split('.');
                                                value = res.data[i];
                                                for (let k in arr) {
                                                    console.log(arr[k])
                                                    value = value[arr[k]];
                                                }
                                            } else {
                                                value = res.data[i][cols[j].field]
                                            }
                                            console.log(value)

                                            if (cols[j].field == 'mobile') {
                                                // value += "\t";
                                            }

                                            if (value == 0) {
                                                value = '0';
                                            }

                                            itemData.push(value)
                                        }
                                    }
                                    data.push(itemData)
                                }


                                // return;
                                table.exportFile(title, data, 'csv');
                            }

                        });

                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: data.field
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    table.reload("data-table", {
                        where: []
                    })
                });
                
                // 字段允许为空
                form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: {
                            field: obj.field,
                            order: obj.type
                        }
                    });
                });

                // 表格新增数据
                let add = function() {
                    layer.open({
                        type: 2,
                        title: "新增",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"500px", common.isModile()?"100%":"450px"],
                        content: INSERT_URL
                    });
                }

                // 表格编辑数据
                let edit = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "修改",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"500px", common.isModile()?"100%":"450px"],
                        content: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                let shipping = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "配货单",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"1000px", common.isModile()?"100%":"750px"],
                        content: SHOW_API + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                let setShippingFee = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "设置物流费用",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"600px", common.isModile()?"100%":"350px"],
                        content: '/app/admin/order/shippingFee' + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                // 删除多行
                let batchRemove = function(obj) {
                    let checkIds = common.checkField(obj, PRIMARY_KEY);
                    if (checkIds === "") {
                        layui.popup.warning("未选中数据");
                        return false;
                    }
                    doRemove(checkIds.split(","));
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function(param) {
                    table.reloadData("data-table", {
                        scrollPos: "fixed"
                    });
                }
            })

            layui.use(["jquery", "xmSelect"], function() {
                layui.$.ajax({
                    url: "/app/admin/goods-brand/get",
                    dataType: "json",
                    success: function (res) {
                        let value = layui.$("#brand_id").attr("value");
                        let initValue = value ? value.split(",") : [];
                        layui.xmSelect.render({
                            el: "#brand_id",
                            name: "brand_id",
                            initValue: initValue,
                            data: res.data,
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true,
                        });
                        if (res.code) {
                            return layui.popup.failure(res.msg);
                        }
                    }
                });
            });

        </script>
    </body>
</html>
